package com.atguigu.hibernate.helloworld;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HibernateTest{
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	@Before
	public void init() {
		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.configure() // configures settings from hibernate.cfg.xml
				.build();
		this.sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();
		this.session = sessionFactory.openSession();
		this.transaction = session.beginTransaction();
	}
	
	@After
	public void destroy() {
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	@Test
	public void testSave() {
		
	}
    
	@Test
	public void testBlob() throws IOException {
		/*News news=new News();
		news.setAuthor("李晓刚");
		news.setContent("今天下雨了");
		news.setDate(new Date());
		news.setTitle("星期五");
		InputStream stream =new FileInputStream("d.jpg");
		Blob image=Hibernate.getLobCreator(session)
				            .createBlob(stream, stream.available());
		news.setImage(image);
		session.save(news);*/
		
		
	}
	@Test
	public void testMany2OneSave() {
		Customer customer1=new Customer();
		customer1.setCustomerName("ee");
		
		Order order1=new Order();
		order1.setOrderName("ORDER-8");
		
		Order order2=new Order();
		order2.setOrderName("ORDER-9");
		
		order1.setCustomer(customer1);
		order2.setCustomer(customer1);
		customer1.getOrders().add(order1);
		customer1.getOrders().add(order2);
		session.save(order1);
		session.save(order2);
		session.save(customer1);
		
	}
	
}
